package GUI;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;

import Entity.User;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

/**
 * @author aviat
 *
 */
public class ViewShifts extends JPanel{

	
	private static final long serialVersionUID = 1L;
	private JButton btnBack;
	private JTable table;
	private JScrollPane scrollPane;
	private JComboBox<String> video1comboBox;
	private JComboBox<String> video2comboBox;
	private JComboBox<String> VideoNumber;
	private JComboBox<String> stills;
	private JComboBox<String> NumberMagnets;
	private JComboBox<String> Magnets1;
	private JComboBox<String> Magnets2;
	private JComboBox<String> HooverCam;
	private JComboBox<String> TeamLeader;

	private ArrayList<User> m_allUsers;
	private Object syncObject;
	
	private boolean m_isChangeAllowed;
	private ArrayList<User> m_filteredUsers;
	/**
	 * Constructor
	 */
	public ViewShifts() {
		
		syncObject = new Object();
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(ViewShifts.class.getResource("/resources/back22.png")));
		scrollPane = new JScrollPane();
		table = new JTable();

		VideoNumber = new JComboBox<String>();
		VideoNumber.setModel(new DefaultComboBoxModel<String>(new String[] {"1", "2"}));
		VideoNumber.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed()){
						if(!(VideoNumber.getSelectedItem()==null)){
							if(VideoNumber.getSelectedItem().toString().equals("1"))
								video2comboBox.setEnabled(false);
							else
								video2comboBox.setEnabled(true);
						}
						//filterComboBox(stills,null,"STILLS");
					}
				}
			}
		});
		video1comboBox = new JComboBox<String>();
		video1comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						filterComboBox(video1comboBox,null,"VIDEO1");
				}
			}
		});
		video2comboBox = new JComboBox<String>();
		video2comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						if(VideoNumber.getSelectedItem().toString().equals("2"))
							filterComboBox(video2comboBox,video1comboBox.getSelectedItem().toString(),"VIDEO2");
				}
			}
		});
		stills = new JComboBox<String>();
		stills.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						filterComboBox(stills,null,"STILLS");
				}
			}
		});
		NumberMagnets = new JComboBox<String>();
		NumberMagnets.setModel(new DefaultComboBoxModel<String>(new String[] {"Yes", "No"}));
		NumberMagnets.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed()){
						if(!(NumberMagnets.getSelectedItem()==null)){
							if(NumberMagnets.getSelectedItem().toString().equals("Yes")){
								Magnets1.setEnabled(true);
								Magnets2.setEnabled(true);
								filterComboBox(Magnets1,NumberMagnets.getSelectedItem().toString(),"MAGNETS1");
							}
							else{
								Magnets1.setEnabled(false);
								Magnets2.setEnabled(false);
							}
						}
					}
					
				}
			}
		});
		Magnets1 = new JComboBox<String>();
		Magnets1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						if(NumberMagnets.getSelectedItem().toString().equals("YES"))
							filterComboBox(Magnets1,null,"MAGNETS1");
				}
			}
		});
		Magnets2 = new JComboBox<String>();
		Magnets2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						if(NumberMagnets.getSelectedItem().toString().equals("YES"))
							filterComboBox(Magnets2,Magnets1.getSelectedItem().toString(),"MAGNETS2");
				}
			}
		});
		HooverCam = new JComboBox<String>();
		HooverCam.setModel(new DefaultComboBoxModel<String>(new String[] {"Yes", "No"}));
		TeamLeader = new JComboBox<String>();
		TeamLeader.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						filterComboBox(TeamLeader,null,"TEAMLEDER");
				}
			}
		});
		
		initialize();
	}

	private void initialize() {
		setLayout(null);
		
		btnBack.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnBack.setBounds(10, 462, 101, 27);
		add(btnBack);
		
		table.setColumnSelectionAllowed(true);
		
		
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null, null, null, null, null, null, null, null},
			},
			new String[] {
				"Date", "Place", "Team Leader", "Stills", "Video Number", "Video 1", "Video 2", "Hover Camera", "Magnets Number", "Magnets 1", "Magnets 2", "Comments", "Contact Name", "Contact Phone"
			}
		));
		table.getColumnModel().getColumn(0).setMinWidth(75);
		table.getColumnModel().getColumn(1).setMinWidth(75);
		table.getColumnModel().getColumn(2).setPreferredWidth(92);
		table.getColumnModel().getColumn(2).setMinWidth(92);
		table.getColumnModel().getColumn(3).setMinWidth(75);
		table.getColumnModel().getColumn(4).setPreferredWidth(100);
		table.getColumnModel().getColumn(4).setMinWidth(100);
		table.getColumnModel().getColumn(5).setPreferredWidth(100);
		table.getColumnModel().getColumn(5).setMinWidth(100);
		table.getColumnModel().getColumn(6).setPreferredWidth(100);
		table.getColumnModel().getColumn(6).setMinWidth(100);
		table.getColumnModel().getColumn(7).setPreferredWidth(94);
		table.getColumnModel().getColumn(7).setMinWidth(94);
		table.getColumnModel().getColumn(8).setPreferredWidth(99);
		table.getColumnModel().getColumn(8).setMinWidth(99);
		table.getColumnModel().getColumn(9).setPreferredWidth(100);
		table.getColumnModel().getColumn(9).setMinWidth(100);
		table.getColumnModel().getColumn(10).setPreferredWidth(100);
		table.getColumnModel().getColumn(10).setMinWidth(100);
		table.getColumnModel().getColumn(11).setPreferredWidth(100);
		table.getColumnModel().getColumn(11).setMinWidth(100);
		table.getColumnModel().getColumn(12).setPreferredWidth(100);
		table.getColumnModel().getColumn(12).setMinWidth(100);
		table.getColumnModel().getColumn(13).setPreferredWidth(100);
		table.getColumnModel().getColumn(13).setMinWidth(100);
		scrollPane.setViewportView(table);
		
		table.setPreferredScrollableViewportSize(new Dimension(500, 500));
		table.setFillsViewportHeight(true);
		scrollPane.setBounds(0, 28, 786, 317);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		add(scrollPane);
		
		TableColumn videoColumn1 = table.getColumnModel().getColumn(5);
		scrollPane.setColumnHeaderView(video1comboBox);
		videoColumn1.setCellEditor(new DefaultCellEditor(video1comboBox));
		
		TableColumn videoColumn2 = table.getColumnModel().getColumn(6);
		videoColumn2.setCellEditor(new DefaultCellEditor(video2comboBox));
		
		TableColumn videonumcol = table.getColumnModel().getColumn(4);
		scrollPane.setColumnHeaderView(VideoNumber);
		videonumcol.setCellEditor(new DefaultCellEditor(VideoNumber));
		
		TableColumn TLeadercol = table.getColumnModel().getColumn(2);
		scrollPane.setColumnHeaderView(TeamLeader);
		//TeamLeader.addItem("");
		TLeadercol.setCellEditor(new DefaultCellEditor(TeamLeader));
		
		TableColumn StillCol = table.getColumnModel().getColumn(3);
		scrollPane.setColumnHeaderView(stills);
		//stills.addItem("");
		StillCol.setCellEditor(new DefaultCellEditor(stills));
		
		TableColumn HcameraCol = table.getColumnModel().getColumn(7);
		scrollPane.setColumnHeaderView(HooverCam);
		HcameraCol.setCellEditor(new DefaultCellEditor(HooverCam));
		
		TableColumn numMagnetCol = table.getColumnModel().getColumn(8);
		scrollPane.setColumnHeaderView(NumberMagnets);
		numMagnetCol.setCellEditor(new DefaultCellEditor(NumberMagnets));
		
		TableColumn magnet1Col = table.getColumnModel().getColumn(9);
		scrollPane.setColumnHeaderView(Magnets1);
		//Magnets1.addItem("");
		magnet1Col.setCellEditor(new DefaultCellEditor(Magnets1));
		
		TableColumn magnet2Col = table.getColumnModel().getColumn(10);
		scrollPane.setColumnHeaderView(Magnets2);
		//Magnets2.addItem("");
		magnet2Col.setCellEditor(new DefaultCellEditor(Magnets2));
	}

	public void setPhotographers(ArrayList<User> allUsers) {
		m_allUsers = allUsers;	
		filterComboBox(video1comboBox,null,"VIDEO1");
		filterComboBox(stills,null,"STILLS");
		filterComboBox(TeamLeader,null,"TEAMLEDER");
//		filterComboBox(HooverCam,null,"HOVER");
//		filterComboBox(VideoNumber,null,"VNUMBER");
//		filterComboBox(NumberMagnets,null,"MAGNETS");
		video2comboBox.setEnabled(false);
		Magnets1.setEnabled(false);
		Magnets2.setEnabled(false);
	}
//	private void filterList(String selectedItem, String scope)
//	{
//		synchronized (syncObject) {
//			m_filteredUsers.clear();
//			
//			switch(scope)
//			{
//			case "VIDEO1":
//				for(User user : m_allUsers)
//				{
//					if(user.getRole().equals("video photografer"))
//					{
//						m_filteredUsers.add(user);
//					}
//				}
//				break;
//			case "LNAME":
//				for(User user : m_allUsers)
//				{
//					if(user.getLName().equals(selectedItem))
//					{
//						m_filteredUsers.add(user);
//					}
//				}
//				break;
//			case "UNAME":
//				for(User user : m_allUsers)
//				{
//					if(String.valueOf(user.getUserName()).equals(selectedItem))
//					{
//						m_filteredUsers.add(user);
//					}
//				}
//			case "ALL":
//				m_filteredUsers.addAll(m_allUsers);
//				break;
//			}
//		}
//		m_isChangeAllowed = false;
//		filterComboBox(WorkerFnamecomboBox,"FNAME");
//		filterComboBox(workerLnamecomboBox,"LNAME");
//		filterComboBox(workerIDcomboBox,"UNAME");
//		m_isChangeAllowed = true;
//
//	}
//	

	private void filterComboBox(JComboBox<String> combo,String selectedItem, String scope)
	{
		m_isChangeAllowed = false;
		combo.removeAllItems();
		synchronized (syncObject) {
			
			for(User user : m_allUsers)
			{
				switch(scope)
				{
				case "VIDEO1":
					if(user.getRole().equals("video photografer"))
						combo.addItem(user.getName()+" "+user.getLName());
					break;
				case "VIDEO2":
					if(user.getRole().equals("video photografer"))
						if(!((user.getName()+" "+user.getLName()).equals(selectedItem)))
							combo.addItem(user.getName()+" "+user.getLName());
					break;
//				case "MAGNETS":
//					combo.addItem("Yes");
//					combo.addItem("No");
//					break;
				case "MAGNETS1":
					if(user.getRole().equals("magnats photografer"))
						combo.addItem(user.getName()+" "+user.getLName());
					break;
				case "MAGNETS2":
					if(user.getRole().equals("magnats photografer"))
						if(!((user.getName()+" "+user.getLName()).equals(selectedItem)))
							combo.addItem(user.getName()+" "+user.getLName());
					break;
				case "STILLS":
					if(user.getRole().equals("stils photografer"))
						combo.addItem(user.getName()+" "+user.getLName());
					break;
				case "TEAMLEDER":
					if(user.getRole().equals("manager"))
						combo.addItem(user.getName()+" "+user.getLName());
					break;
//				case "HOVER":
//						combo.addItem("Yes");
//						combo.addItem("No");
//					break;
//				case "VNUMBER":
//					combo.addItem("1");
//					combo.addItem("2");
				//break;
//				case "UNAME":
//					combo.addItem(String.valueOf(user.getUserName()));
//					break;
				}
			}
		}
		m_isChangeAllowed = true;
	}
	
	private boolean isChangeAllowed()
	{
		return m_isChangeAllowed;
	}
	/**
	 * @return the btnBack
	 */
	public JButton getBtnBack() {
		return btnBack;
	}

	/**
	 * @param btnBack the btnBack to set
	 */
	public void setBtnBack(JButton btnBack) {
		this.btnBack = btnBack;
	}

	/**
	 * @return the table
	 */
	public JTable getTable() {
		return table;
	}

	/**
	 * @param table the table to set
	 */
	public void setTable(JTable table) {
		this.table = table;
	}
}
